package com.mrjy.maintain.bo;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mrjy.common.entity.BannerInfo;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.util.GlobalConstant;
import com.mrjy.maintain.dao.IBannerInfoDao;

@Service
public class BannerInfoBo implements IBannerInfoBo {

	@Autowired
	private IBannerInfoDao bannerInfoDao;
	
	@Override
	public Object insert(BannerInfo bannerInfo,HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(null == loginUser ){
			map.put("err_code", -1);
			map.put("err_msg", "用户未登录");
			return map;
		}
		//数据校验
		if (StringUtils.isBlank(bannerInfo.getImg())) {
			map.put("err_code", 404);
			map.put("err_msg", "封面不能为空");
			return map;
		}
		if (StringUtils.isBlank(bannerInfo.getUrl())) {
			map.put("err_code", 401);
			map.put("err_msg", "链接不能为空");
			return map;
		}
		if (StringUtils.isBlank(bannerInfo.getLocation())) {
			map.put("err_code", 402);
			map.put("err_msg", "广告位置不能为空");
			return map;
		}
		if (StringUtils.isBlank(bannerInfo.getSort())) {
			map.put("err_code", 403);
			map.put("err_msg", "顺序不能为空");
			return map;
		}
		
		//设置更新时间
		bannerInfo.setUpdateTime(new Timestamp(new Date().getTime()));
		int row = bannerInfoDao.insert(bannerInfo);
		if (row>0) {
			map.put("err_code", 200);
			map.put("err_msg", "添加成功");
		}else {
			map.put("err_code", 500);
			map.put("err_msg", "添加失败");
		}
		return map;
	}

	@Override
	public Object queryByPage(BannerInfo bannerInfo,HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(null == loginUser ){
			map.put("err_code", -1);
			map.put("err_msg", "用户未登录");
			return map;
		}
		int total = bannerInfoDao.queryCount(bannerInfo);
		if (total > 0) {
			List<BannerInfo> list = bannerInfoDao.queryByPage(bannerInfo);
			map.put("err_code", 200);
			map.put("err_msg", "查询成功,有数据");
			map.put("total", total);
			map.put("rows", list);
		}else {
			map.put("err_code", 500);
			map.put("err_msg", "查询成功,无数据");
			map.put("total", total);
			map.put("rows", null);
		}
		return map;
	}

	@Override
	public Object updateDisableFlag(BannerInfo bannerInfo,HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(null == loginUser ){
			map.put("err_code", -1);
			map.put("err_msg", "用户未登录");
			return map;
		}
		if (bannerInfo==null) {
			map.put("err_code", 400);
			map.put("err_msg", "参数异常");
			return map;
		}
		Integer row = bannerInfoDao.updateDisableFlag(bannerInfo);
		if (row!=null && row>0) {
			map.put("err_code", 200);
			map.put("err_msg", "操作成功");
		}else {
			map.put("err_code", 500);
			map.put("err_msg", "操作失败");
		}
		return map;
	}

	@Override
	public Object queryById(BannerInfo bannerInfo) {
		return bannerInfoDao.queryById(bannerInfo);
	}

	@Override
	public Object updateById(BannerInfo bannerInfo,HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(null == loginUser ){
			map.put("err_code", -1);
			map.put("err_msg", "用户未登录");
			return map;
		}
		if (StringUtils.isBlank(bannerInfo.getImg())) {
			map.put("err_code", 404);
			map.put("err_msg", "封面不能为空");
			return map;
		}
		if (StringUtils.isBlank(bannerInfo.getUrl())) {
			map.put("err_code", 401);
			map.put("err_msg", "链接不能为空");
			return map;
		}
		if (StringUtils.isBlank(bannerInfo.getLocation())) {
			map.put("err_code", 402);
			map.put("err_msg", "广告位置不能为空");
			return map;
		}
		if (StringUtils.isBlank(bannerInfo.getSort())) {
			map.put("err_code", 403);
			map.put("err_msg", "顺序不能为空");
			return map;
		}
		//设置更新时间
		bannerInfo.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		int row = bannerInfoDao.updateById(bannerInfo);
		if (row>0) {
			map.put("err_code", 200);
			map.put("err_msg", "修改成功");
		}else {
			map.put("err_code", 500);
			map.put("err_msg", "修改失败");
		}
		return map;
	}

	@Override
	public Object addClickCount(Integer id) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (id==null) {
			map.put("err_code", 400);
			map.put("err_msg", "参数异常");
			return map;
		}
		int row = bannerInfoDao.addClickCount(id);
		if (row>0) {
			map.put("err_code", 0);
			map.put("err_msg", "更新点击量成功");
		}else {
			map.put("err_code", -1);
			map.put("err_msg", "更新点击量失败");
		}
		return map;
	}

	@Override
	public Object queryBannerList(BannerInfo bannerInfo) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(StringUtils.isBlank(bannerInfo.getLocation())){
			map.put("err_code", 400);
			map.put("err_msg", "参数异常");
			return map;
		}
		List<BannerInfo> list = bannerInfoDao.queryByPage2(bannerInfo);
			if (list != null && list.size()>0) {
			map.put("err_code", 0);
			map.put("err_msg", "查询成功,有数据");
		}else {
			map.put("err_code", 1);
			map.put("err_msg", "查询无数据");
		}
		map.put("rows",list);
		return map;
	}


}
